library(htmltools) ## HTML()
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(dtplyr)
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
library(magrittr)
library(scales)
library(grattanCharts)  # devtools::install_github('hughparsonage/grattanCharts')
## 
## Attaching package: 'grattanCharts'
## The following object is masked from 'package:datasets':
## 
##     Orange
library(grattan)
library(rvest)
## Loading required package: xml2
library(xml2)
library(httr)
library(leaflet)
library(ASGS)
## Loading required package: rgdal
## Loading required package: sp
## rgdal: version: 1.2-7, (SVN revision 660)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
##  Path to GDAL shared files: C:/Users/hughp/Documents/R/win-library/3.4/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
##  Path to PROJ.4 shared files: C:/Users/hughp/Documents/R/win-library/3.4/rgdal/proj
##  Linking to sp version: 1.2-4
## 
## Attaching package: 'rgdal'
## The following object is masked from 'package:grattan':
## 
##     project
Repayment_stress_by_SA2 <- 
  fread("~/Data/ABS/Mortgage-payment-stress.csv", na.strings = "--") %>% 
  .[`Mortgage monthly repayments` == "Households with mortgage repayments greater than 30% of household income"] %>% 
  setorderv("%") %>% 
  setnames("%", "Prop_geq_30pc") %>%
  .[SA2016_decoder, on = c("Suburb==SA2_NAME16")] %>%
  select(SA2_MAIN16, Prop_geq_30pc) %>%
  mutate(SA2_MAIN16 = factor(SA2_MAIN16))


SA2_2016_join <- SA2_2016
SA2_2016_join@data <-
  inner_join(SA2_2016_join@data, Repayment_stress_by_SA2, by = "SA2_MAIN16")
## Warning in inner_join_impl(x, y, by$x, by$y, suffix$x, suffix$y): joining
## factors with different levels, coercing to character vector
pal_v <- colorNumeric(palette = gpal(7, reverse = TRUE),
                      domain = NULL)
## I'm going off-piste: The Palette Of Nine is thine. May John have mercy on your soul.
SA2_2016_simple %>%
  leaflet %>%
  addPolygons(stroke = TRUE,
              opacity = 1,
              weight = 1,  # the border thickness / pixels
              color = "black",
              fillColor = ~pal_v(SA2_2016_join@data$Prop_geq_30pc), 
              fillOpacity = 1,
              label = lapply(paste0("<b>", 
                                    SA2_2016_join@data$SA2_NAME16, ":",
                                    "</b><br>",
                                    SA2_2016_join@data$Prop_geq_30pc,
                                    "%"),
                             HTML),
              highlightOptions = highlightOptions(weight = 2,
                                                  color = "white",
                                                  opacity = 1,
                                                  dashArray = "",
                                                  bringToFront = TRUE)) %>%
  addLegend(pal = pal_v, 
            opacity = 1,
            values = ~SA2_2016_join@data$Prop_geq_30pc,
            labFormat = labelFormat(suffix = "%"),
            title = "% households under mortgage stress")